package com.hzsd.zengbanyi.biaodan_text.fragment;

import java.util.ArrayList;
import java.util.List;

import hzst.android.util.JsonDataParser;
import hzst.android.util.NetworkDataUtil;

import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.hzsd.zengbanyi.biaodan_text.MyAdapter.LabSampleAdapter;
import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.LabSampleBean;
import com.hzsd.zengbanyi.biaodan_text.bean.LabSampleBodyBean;

/**
 * 原始记录单
 * 
 * @author Administrator
 * 
 */
public class OriginalRecordFragment extends Fragment {
	private View view;
	private ListView listView;
	private LabTopLeftFragment labTopLeftFragment;
	private LabTopRightFragment labTopRightFragment;
	private FragmentManager fm;
	private FragmentTransaction ft;
	private LabSampleBean labSampleBean, labSampleTitleBean;
	private List<LabSampleBodyBean> bodyBeans, bodyTitleBeans;
	private LabSampleAdapter adapter;
	private View dialogView;
	private String urlPath;

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				if (labSampleBean==null) {
					break;
				}
				Bundle bundle = new Bundle();
				bundle.putSerializable("LabSampleBean", labSampleBean);
				ft.replace(R.id.fl_top_left, labTopLeftFragment);
				labTopLeftFragment.setArguments(bundle);
				ft.replace(R.id.fl_top_right, labTopRightFragment);
				labTopRightFragment.setArguments(bundle);
				ft.commit();

				bodyBeans = new ArrayList<LabSampleBodyBean>();
				bodyBeans = labSampleBean.getBody();
				if (adapter == null) {
					adapter = new LabSampleAdapter(getActivity(), bodyBeans);
					listView.setAdapter(adapter);
				} else {
					adapter.notifyDataSetChanged();
				}
				break;
//			case 1:
//				bodyTitleBeans = new ArrayList<LabSampleBodyBean>();
//				bodyTitleBeans = labSampleTitleBean.getBody();
//				getData();
//				break;

			default:
				break;
			}
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_origina_record, container, false);
		fm = getActivity().getSupportFragmentManager();
		ft = fm.beginTransaction();
		initBaseData();
		initView(view);
		initEvents();
		getData();
		return view;
	}

	private void initBaseData() {
		SharedPreferences sp = getActivity().getSharedPreferences("user", getActivity().MODE_PRIVATE);
		urlPath = sp.getString("urlPath", "");
		
	}

	private void initView(View view) {
		listView = (ListView) view.findViewById(R.id.listview);

	}
	
	private void initEvents() {
		labTopLeftFragment = new LabTopLeftFragment();
		labTopRightFragment = new LabTopRightFragment();
		
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_lab_sample_value, null, false);
				View view2 = dialogView.findViewById(R.id.fl_dialog_lab);
				
				final EditText et_Volume = (EditText) dialogView.findViewById(R.id.et_Volume);
				final EditText et_Dilution = (EditText) dialogView.findViewById(R.id.et_Dilution);
				final EditText et_A0 = (EditText) dialogView.findViewById(R.id.et_A0);
				final EditText et_A = (EditText) dialogView.findViewById(R.id.et_A);
				final EditText et_A_Ao = (EditText) dialogView.findViewById(R.id.et_A_Ao);
				final EditText et_Curve = (EditText) dialogView.findViewById(R.id.et_Curve);
				final EditText et_concentration = (EditText) dialogView.findViewById(R.id.et_concentration);
				final EditText et_AverageConcentration = (EditText) dialogView.findViewById(R.id.et_AverageConcentration);
				
				//自己动获取上一条实验数据的值为下一条的默认值
				int index = 0;
				if (position>0) {
					index = position - 1;
				}
				et_Volume.setText(bodyBeans.get(index).getVolume());
				et_Dilution.setText(bodyBeans.get(index).getDilution());
				et_A0.setText(bodyBeans.get(index).getA0());
				et_A.setText(bodyBeans.get(index).getA());
				/////////////////
				
				et_A_Ao.setText(bodyBeans.get(position).getA_Ao());
				et_Curve.setText(bodyBeans.get(position).getCurve());
				et_concentration.setText(bodyBeans.get(position).getConcentration());
				et_AverageConcentration.setText(bodyBeans.get(position).getAverageConcentration());
				
				Builder builder = new Builder(getActivity());
				builder.setTitle(bodyBeans.get(position).getCode());
				builder.setView(dialogView);
				builder.setNegativeButton("取消", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
				builder.setPositiveButton("保存", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						bodyBeans.get(position).setVolume(et_Volume.getText().toString());
						bodyBeans.get(position).setDilution(et_Dilution.getText().toString());
						bodyBeans.get(position).setA0(et_A0.getText().toString());
						bodyBeans.get(position).setA(et_A.getText().toString());
						bodyBeans.get(position).setA_Ao(et_A_Ao.getText().toString());
						bodyBeans.get(position).setCurve(et_Curve.getText().toString());
						bodyBeans.get(position).setConcentration(et_concentration.getText().toString());
						bodyBeans.get(position).setAverageConcentration(et_AverageConcentration.getText().toString());
						adapter.notifyDataSetChanged();
						dialog.dismiss();
						
					}
				});
				builder.create().show();
			}
		});
		
	}

	private void getData() {
		final String url = "http://"+urlPath+"/ServiceInterface/OriginalRecord/Test.ashx";
		new Thread(){
			public void run() {
				String json = NetworkDataUtil.getJsonData(url);
				labSampleBean = new LabSampleBean();
				labSampleBean = JsonDataParser.getBean(json, LabSampleBean.class);
				Message msg = Message.obtain();
				msg.what = 0;
				handler.sendMessage(msg);
			};
		}.start();
		
	}


}
